package com.ziyougou.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.ziyougou.product.entity.ProductAttrValueEntity;
import com.ziyougou.product.service.ProductAttrValueService;
import com.ziyougou.product.vo.AttrResponseVO;
import com.ziyougou.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ziyougou.product.entity.AttrEntity;
import com.ziyougou.product.service.AttrService;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.R;



/**
 * 商品属性
 *
 * @author yuanhaipeng
 * @email yuanhaipeng@gmail.com
 * @date 2024-06-26 14:09:53
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService attrValueService;
    //根据spuid修改规格
    @PostMapping("update/{spuid}")
    public R update(@PathVariable("spuid")Long spuid,@RequestBody List<ProductAttrValueEntity> attrList){
        attrValueService.updateAttr(spuid,attrList);
        return  R.ok();
    }

    //根据spuid查询spu对应的属性列表-基础属性
    @GetMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId")Long spuId){
       List<ProductAttrValueEntity> attrList= attrValueService.listforsupBySpuId(spuId);
        return R.ok().put("data",attrList);
    }


    /**
     * 查询的是属性中的基本属性+查询销售属性
     * base/list/225
     * sale/list/225
     */
    @RequestMapping("/{type}/list/{catId}")
   // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId")Long catId,@PathVariable("type")String type){
        PageUtils page = attrService.queryPage(params,catId,type);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
   // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
       AttrResponseVO responseVO= attrService.getAttrById(attrId);

        return R.ok().put("attr", responseVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attr){
		//attrService.save(attr);
        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attr){
		//attrService.updateById(attr);
        attrService.updateAttrById(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
